<?php
declare(strict_types=1);
 

namespace App\Fastapp\Mapper;

use App\Fastapp\Model\FastappTag;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 标签列表Mapper类
 */
class FastappTagMapper extends AbstractMapper
{
    /**
     * @var FastappTag
     */
    public $model;

    public function assignModel()
    {
        $this->model = FastappTag::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 场景101corp701game801ex
        if (isset($params['scene']) && filled($params['scene'])) {
            if (!is_array($params['scene'])) $params['scene'] = [$params['scene']];
            $query->whereIn('scene', $params['scene']);
        }
        return $query->orderByDesc('id');
    }

    public function select(array $param): array
    {
        if (!is_array($param['scene'])) $param['scene'] = [$param['scene']];
        return $this->model::query()->whereIn('scene', $param['scene'])->select(['id', 'name'])->get()->map(function ($item) {
            $item->name = fastapp_lang_text($item->name);
            return $item;
        })->toarray();
    }
}